package com.tedu.egmvcwork.controller;

import com.tedu.egmvcwork.mapper.AddressMapper;
import com.tedu.egmvcwork.pojo.dto.AddressDTO;
import com.tedu.egmvcwork.pojo.entity.Address;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;

    @RequestMapping("/address/insert")
    @ResponseBody
    public String insert(AddressDTO addressDTO){
        System.out.println(addressDTO);
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO,address);
        System.out.println(address);
        int num = addressMapper.insert(address);
        return num>0?"success":"fail";
    }
    @RequestMapping("/address/update")
    @ResponseBody
    public String update(AddressDTO addressDTO){
        System.out.println(addressDTO);
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO,address);
        int num = addressMapper.update(address);
        return num>0?"success":"fail";
    }
    @RequestMapping("/address/delete")
    @ResponseBody
    public String delete(int id){
        int num = addressMapper.delete(id);
        return num>0?"success":"fail";
    }
    @RequestMapping("/address/select")
    @ResponseBody
    public Address select(String receiver){
        Address address = addressMapper.select(receiver);
        return address;
    }
    @RequestMapping("/address/selectAll")
    @ResponseBody
    public List<Address> selectAll(){
        List<Address> receiver = addressMapper.selectAll();
        return receiver;
    }


}
